package cn.sibu.disalllowtouchintercept;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by 刘红亮 on 15-12-24 下午3:05.
 */
public class WebImageView extends ImageView {

    private Drawable mPlaceholder,mImage;

    public WebImageView(Context context) {
        this(context,null);
    }

    public WebImageView(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }

    public WebImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    public void setPlaceholderImage(Drawable drawable){
        mPlaceholder = drawable;
        if(mImage == null){
            setImageDrawable(mPlaceholder);
        }
    }

    public void setPlaceholderImage(int resid){
        mPlaceholder = getResources().getDrawable(resid);
        if(mImage == null){
            setImageDrawable(mPlaceholder);
        }
    }

    public void setImageUrl(String url){
        DownloadTask task = new DownloadTask();
        task.execute(url);
    }


    private class DownloadTask extends AsyncTask<String,Void,Bitmap>{

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
            Log.e("onProgressUpdate","onProgressUpdate"+values);
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            String url = params[0];

            try {
                URLConnection connection =  (new URL(url)).openConnection();
                InputStream is = connection.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);

               // ByteArrayBuffer baf = new ByteArrayBuffer(50);已过时
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte []by = new byte[1024];
                int current = 0;
                while ((current = bis.read(by))!= -1){
                   // baf.append((byte) current);已过时
                    bos.write(by,0,current);
                }
                bos.flush();
                //byte[] imageData = baf.toByteArray();已过时
                byte[] imageData = bos.toByteArray();
                bos.close();
                bis.close();
               return BitmapFactory.decodeByteArray(imageData,0,imageData.length);

            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            mImage = new BitmapDrawable(getContext().getResources(),bitmap);
            if(mImage!=null){
                setImageDrawable(mImage);
            }
        }
    }
}
